package com.example.dtos;

import java.sql.Date;
import java.text.SimpleDateFormat;

import javax.persistence.Id;
import javax.validation.constraints.NotNull;

import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.hateoas.ResourceSupport;

import com.example.exceptions.MissingAttributesException;
import com.example.exceptions.WrongAttributeTypeException;
import com.example.models.Product;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class ProductDTO extends ResourceSupport{
	//info of hibernate annotations and validations:
	//http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-obtaining-validator
	private String id; //THIS IS WITH MONGODB
//	private Long id; //mysql
//	private String category,name;
//	private double price;
//	private int stock;
//	@Future
//	private Date expirationDate;
//	@Past
//	private Date creationDate;
	@NotNull(message="data cannot be null")
	private String data;
	
	public ProductDTO(){}
	
	public ProductDTO(String id){
//	public ProductDTO(Long id){ //mysql
		this.id=id;
	}
	
	@JsonCreator
	public ProductDTO(@JsonProperty("id") String id,@JsonProperty("data") String data){
		this.id=id;
		this.data=data;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}
	
//	public ProductDTO(String id, String category, String name, double price, int stock) {
//	public ProductDTO(Long id, String category, String name, double price, int stock) {  //mysql
//		super();
//		this.id = id;
//		this.category = category;
//		this.name = name;
//		this.price = price;
//		this.stock = stock;
//	}
//
//	public ProductDTO(String category, String name, double price, int stock) {
//		super();
//		this.category = category;
//		this.name = name;
//		this.price = price;
//		this.stock = stock;
//	}
//	
//	public static void checkFields(ProductDTO productDTO){
//		if( (productDTO.getCategory()==null) || (productDTO.getName()==null))
//			throw new MissingAttributesException();
//	 }
	
	public static ProductDTO checkData(ProductDTO productDTO){
		String[] parts = productDTO.getData().split("/");
		if(parts.length<5) throw new MissingAttributesException();
		try{
			Double.parseDouble(parts[2]);
			Integer.parseInt(parts[3]);
			SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
			formatter.parse(parts[4]);
		}catch(Exception e){
			throw new WrongAttributeTypeException();
		}
		return productDTO;
	}
	
//	public Long getId() {
//		return id;
//	}
//
//	public void setId(Long id) {
//		this.id = id;
//	}

//	public String getId() {
//		return id;
//	}
//	public void setId(String id) {
//		this.id = id;
//	}
//	public String getCategory() {
//		return category;
//	}
//	public void setCategory(String category) {
//		this.category = category;
//	}
//	public String getName() {
//		return name;
//	}
//	public void setName(String name) {
//		this.name = name;
//	}
//	public double getPrice() {
//		return price;
//	}
//	public void setPrice(double price) {
//		this.price = price;
//	}
//	public int getStock() {
//		return stock;
//	}
//	public void setStock(int stock) {
//		this.stock = stock;
//	}
	
	
}
